<template>
  <div>
    <el-card class="box-card">
      <el-table
        style="margin-bottom:20px"
        :header-cell-style="{background:'#eee'}"
        border
        :data="tableData"
      >
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column align="center" prop="title" label="描述"></el-table-column>
        <el-table-column align="center" prop="integral" label="产生积分"></el-table-column>
        <el-table-column align="center" label="类型">
          <template slot-scope="scope">
            <div v-if="scope.row.in_type==1">收入</div>
            <div v-if="scope.row.in_type==2">支出</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="surplus_integral" label="剩余积分"></el-table-column>
        <el-table-column align="center" prop="add_time" label="时间"></el-table-column>
      </el-table>
      <div style="text-align:right">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="total"
          @current-change="currentChange"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { userIntegral } from "@/api/consumer";
import { parseTime } from "@/utils/index";
export default {
  props: {
    user_id: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      // 分页数据
      total: 0,
      currentPage: 1,
      pageSize: 10,
      tableData: []
    };
  },
  mounted() {
    // this.getUserCoupon();
  },
  watch: {
    user_id(newVal, oldVal) {
      // this.getUserIntegral();
    }
  },
  methods: {
    // 获取优惠券列表
    async getUserIntegral() {
      const res = await userIntegral({
        user_id: this.user_id,
        page: this.currentPage,
        limit: this.pageSize
      });
      console.log(res);
      this.tableData = res.data;
      this.tableData.forEach(item => {
        item.add_time = parseTime(item.add_time, "{y}-{m}-{d} {h}:{i}:{s}");
      });
      this.total = res.count;
    },
    queryAsk() {
      this.currentPage = 1;
      this.getUserIntegral();
    },
    //重置
    queryReset() {
      this.resetForm("query");
      this.total = 0;
      this.currentPage = 1;
      this.pageSize = 10;
      this.query.keywords = "";
      this.getUserIntegral();
    },

    //分页改变
    currentChange(current) {
      this.currentPage = current;
      this.getUserIntegral();
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang='scss' scoped>
.box-card {
  margin-bottom: 20px;
}
.search {
  display: flex;
  align-items: center;
}
::v-deep .el-input {
  width: 292px;
  display: block;
  position: static !important;
}
::v-deep .el-input--medium .el-input__inner {
  width: 292px;
}

::v-deep .el-form-item__label {
  padding: 0 30px 0 0;
}
</style>